#-----------------------------------------------------------------------------
# Name:        SourceBackup.py
# Purpose:     
#
# Author:      Luigi Poderico
#
# Created:     2005/04/12
# RCS-ID:      $Id: SourceBackup.py $
# Copyright:   (c) 2002
# Licence:     <your licence>
#-----------------------------------------------------------------------------

import ConfigParser

class SourceBackup:
    """
    Ask one to the user the filesystem sub-tree to backup. The user choice is
    stored on the working directory.
    """
    
    __theConfigParser = './EasyBackup.ini'
    __SectionGeneral = "General"
    __SourceBackupOption = "SourceBackUp"

    
    def __GetSourceFromIniFile(self):
        """
        Return the root file-system tree to back-up if any. Otherwise
        return "".
        """
                
        myPathList = []
        
        try:
            myConfigParser = ConfigParser.RawConfigParser()
            myConfigParser.read(self.__theConfigParser)
            myPathsRepr = myConfigParser.get(self.__SectionGeneral, self.__SourceBackupOption)
            myPathList = eval(myPathsRepr)
            pass
        except:
            pass
        
        if myPathList == None:
            myPathList = []
            pass
        
        return myPathList
    
#-------------------------------------------------------------------------------
    
    def __GetSourceFromUser(self):
        """
        Ask to the user a path.
        """
        
        myPathList = []

        try:
            from wxPython.wx import wxDirDialog
            from wxPython.wx import wxID_OK
                        
            while True:
                dlg = wxDirDialog(None, "Select the directory to backup.")
                myPath = ""
                try:
                    if dlg.ShowModal() == wxID_OK:
                        myPath = dlg.GetPath()
                        myPathList.append(myPath)
                    else:
                        break
                finally:
                    dlg.Destroy()
                pass
            pass
        except:
            # Read from keyboard
            while True:
                myPath = raw_input("Select the directory to backup:")
                if myPath=="":
                    break
                myPathList.append(myPath)
                pass
            pass
        
        return myPathList
    
#-------------------------------------------------------------------------------
    
    def __SaveSourceInIniFile(self, aPathList):
        """
        Store in configuration aPathList.
        """
        
        myConfigParser = ConfigParser.RawConfigParser()
        myConfigParser.read(self.__theConfigParser)
        if not myConfigParser.has_section(self.__SectionGeneral):
            myConfigParser.add_section(self.__SectionGeneral)
            pass
        myPathListRepr = repr(aPathList)
        myConfigParser.set(self.__SectionGeneral, self.__SourceBackupOption, myPathListRepr)
        myConfigParser.write(open(self.__theConfigParser, 'w'))
        pass

#-------------------------------------------------------------------------------
    
    def GetSourceBackup(self):
        """
        Return a root file-system tree choosed by the user.
        """
        
        myPath = self.__GetSourceFromIniFile()
        
        if myPath == []:
            myPath = self.__GetSourceFromUser()
            self.__SaveSourceInIniFile(myPath)
            pass
        
        
        return myPath
    
    pass ## class